package com.ugrow.ugrowcommunity.activity.courseevaluation;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ugrow.ugrowcommunity.databinding.ActivityEvaluationBinding;

public class EvaluationActivity extends AppCompatActivity{

    private ActivityEvaluationBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityEvaluationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        changeScore();

        setTeacherData();

        //返回
        binding.imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //确定
        binding.evaluationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹窗
                showDialog();
            }
        });
    }
    private void showDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(EvaluationActivity.this);
        builder.setMessage("评价后不可更改，确认评价吗？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(EvaluationActivity.this, "评价成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
        builder.create().show();
    }

    //设置老师名字和课程
    private void setTeacherData(){
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String course = intent.getStringExtra("course");
        String stucourse = intent.getStringExtra("stucourse");
        binding.tvCourse.setText(course+":"+stucourse);
        binding.tvTeacher.setText("授课教师:"+name);
    }

    //点击星星改变分数
    private void changeScore(){
        binding.ratindBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                countNum(rating,binding.score1);
            }
        });
        binding.ratindBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                countNum(rating,binding.score2);
            }
        });
        binding.ratindBar3.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                countNum(rating,binding.score3);
            }
        });
        binding.ratindBar4.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                countNum(rating,binding.score4);
            }
        });
        binding.ratindBar5.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                countNum(rating,binding.score5);
            }
        });
    }

    //封装一个计算方法
    private void countNum(float number, TextView textView){
        int score = (int) (number * 20);
        textView.setText(score+"");
    }
}